package com.zhuang.demo;

import com.zhuang.demo.dao.BookMapper;
import com.zhuang.demo.dao.BookTypeMapper;
import com.zhuang.demo.entity.Book;
import com.zhuang.demo.entity.BookType;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @className: TT
 * @description: 类描述：
 * @author: LiZhuang
 * @date: 2021/3/9 14:17
 */
public class TT {
    @Test
    public  void  select(){
        //流地址
        String resource = "mybatis-config.xml";
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = build.openSession();
            List<Book> books = sqlSession.getMapper(BookMapper.class).selectBookAndBookType();
            System.out.println(books);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public  void select2(){
        String resource="mybatis-config.xml";
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
            List<Book> books=sqlSession.getMapper(BookMapper.class).selectBookAndBookType2();
            for (Book book : books) {
                System.out.println(book);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public  void select3(){
        String resource="mybatis-config.xml";
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
            List<Book> books=sqlSession.getMapper(BookMapper.class).selectBookAndBookType3();
            for (Book book : books) {
                System.out.println(book);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void select4(){
        String resource="mybatis-config.xml";
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
            List<BookType> bookTypeList=sqlSession.getMapper(BookTypeMapper.class).findBookTypes();
            sqlSession.close();
            for (BookType bookType : bookTypeList) {
                System.out.println(bookType);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
